package cn.kgc.rbac.controller;

import cn.kgc.rbac.common.CodeMsg;
import cn.kgc.rbac.common.ResponseResult;
import cn.kgc.rbac.entity.Permission;
import cn.kgc.rbac.service.PermissionService;
import cn.kgc.rbac.vo.PermissionVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: mosin
 * @version: v1.0  2023/2/2
 */
@RestController
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("list")
    public ResponseResult  getList(){

        List<Permission> list = permissionService.list();
        List<PermissionVO> collect = list.stream().map(permission -> {
            PermissionVO permissionVO = new PermissionVO();
            BeanUtils.copyProperties(permission, permissionVO);
            return permissionVO;
        }).collect(Collectors.toList());
        return new ResponseResult(CodeMsg.SUCCESS,collect,null);
    }

    @RequestMapping("treeSelect")
    public ResponseResult  treeSelect(){
        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.in(Permission::getIsMenu, Arrays.asList(0,1));
        List<Permission> list = permissionService.list(lambda);
        return  new ResponseResult(CodeMsg.SUCCESS,list,null);
    }


    @RequestMapping("add")
    public ResponseResult addPermission(Permission permission){
        permissionService.save(permission);
        return new ResponseResult(CodeMsg.SUCCESS);
    }

    // 删除权限
    @RequestMapping("delete")
    public ResponseResult  deleteById(Integer id){
       return permissionService.deleteById(id);
    }

    // 根据id查询权限信息
    @RequestMapping("selectById")
    public ResponseResult  selectById(Integer id){

        Permission permission = permissionService.getById(id);
        return new ResponseResult(CodeMsg.SUCCESS,permission,null);
    }


    @RequestMapping("update")
    public ResponseResult  update(Permission permission){

        permissionService.updateById(permission);

        return new ResponseResult(CodeMsg.SUCCESS);
    }

}
